package com.waves.message.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.waves.core.constant.DbConstants;
import com.waves.core.support.EntityOperations;
import com.waves.message.dto.MessageTemplateInsertDTO;
import com.waves.message.entity.MessageTemplate;
import com.waves.message.mapper.MessageMapper;
import com.waves.message.service.MessageService;
import com.waves.message.vo.MessageTemplateVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @author wzw
 * @description 针对表【message】的数据库操作Service实现
 * @createDate 2025-01-01 15:01:07
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, MessageTemplate>
        implements MessageService {

    @Override
    public MessageTemplateVO getMessageByCode(String code) {
        MessageTemplate messageTemplate = baseMapper.selectOne(
                Wrappers.lambdaQuery(MessageTemplate.class)
                        .eq(MessageTemplate::getCode, code));
        MessageTemplateVO target = new MessageTemplateVO();
        BeanUtils.copyProperties(messageTemplate, target);
        return target;
    }

    @Override
    public Long insert(MessageTemplateInsertDTO messageTemplateInsertDTO) {
        MessageTemplate entity = new MessageTemplate();
        BeanUtils.copyProperties(messageTemplateInsertDTO, entity);
        Optional<MessageTemplate> execute = EntityOperations.doCreate(baseMapper)
                                                            .create(() -> entity)
                                                            .update(e -> {})
                                                            .execute();
        return execute.map(MessageTemplate::getId)
                      .orElse(DbConstants.DEFAULT_LONG_VALUE);
    }
}




